package com.messi.languagehelper.http;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import com.messi.languagehelper.util.LogUtil;

import android.content.Context;

public class LanguageHelperHttp extends AbstractHttpApi {

	private DefaultHttpClient httpClient;

	public LanguageHelperHttp() {
		httpClient = AbstractHttpApi.createHttpClient();
	}
	
	public void post(int cId, String mJson, Context mContext) throws UnsupportedEncodingException, Exception {
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("msgId", cId+"");
		map.put("req", mJson);
	}

	public String doPost(String path, Map<String, String> params, String encoding, Context mContext) throws UnsupportedEncodingException, IOException {
		LogUtil.DefalutLog("doPost");
		List<NameValuePair> param = paramNameValuePair(params);
		UrlEncodedFormEntity entity = new UrlEncodedFormEntity(param, encoding);
		HttpPost post = new HttpPost(path);
		post.setEntity(entity);
		HttpResponse response = executeHttpRequest(post);
		int code = response.getStatusLine().getStatusCode();
		if (code == 200) {
			String content = EntityUtils.toString(response.getEntity(), encoding);
			return content;
		} else {
			
			return null;
		}
	}

	public String doGet(String path, Map<String, String> params, String encoding) throws UnsupportedEncodingException, IOException {
		LogUtil.DefalutLog("doGet");
		List<NameValuePair> param = paramNameValuePair(params);
		String query = URLEncodedUtils.format(param, HTTP.UTF_8);
		HttpGet get = new HttpGet(path + "?" + query);
		HttpResponse response = executeHttpRequest(get);
		if (response.getStatusLine().getStatusCode() == 200) {
			String content = EntityUtils.toString(response.getEntity(), encoding);
			return content;
		} else {
			return null;
		}
	}

	/**
	 * change Map into NameValuePair.
	 * 
	 * @param List
	 *            <NameValuePair>
	 */
	public List<NameValuePair> paramNameValuePair(Map<String, String> params) {
		List<NameValuePair> param = new ArrayList<NameValuePair>();
		if (params != null && !params.isEmpty()) {
			for (Map.Entry<String, String> entry : params.entrySet()) {
				param.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
			}
		}
		return param;
	}

	/**
	 * execute() an httpRequest catching exceptions and returning null instead.
	 * 
	 * @param httpRequest
	 * @throws IOException
	 */
	public HttpResponse executeHttpRequest(HttpRequestBase httpRequest) throws IOException {
		LogUtil.DefalutLog("executing HttpRequest for: " + httpRequest.getURI().toString());
		try {
			httpClient.getConnectionManager().closeExpiredConnections();
			return httpClient.execute(httpRequest);
		} catch (IOException e) {
			httpRequest.abort();
			throw e;
		}
	}
}
